package com.hbk.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hbk.base.common.CommonResult;
import com.hbk.base.common.DataGrid;
import com.hbk.base.common.Ref;
import com.hbk.base.util.TimeUtil;
import com.hbk.entity.Consultation;
import com.hbk.mapper.ConsultationMapper;
import com.hbk.param.ConParam;
import com.hbk.service.ICacheService;
import com.hbk.service.IConsultationService;
import com.hbk.service.IOssService;
import com.hbk.vo.ConVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ConsultationServiceImpl implements IConsultationService {

    @Resource
    private ConsultationMapper consultationMapper;
    @Resource
    private IOssService ossService;
    @Resource
    private ICacheService cacheService;

    /** 新增 */
    public CommonResult add(ConParam.AddParam param){
        Consultation consultation = new Consultation();
        consultation.setImgUrl(param.getImgUrl());
        consultation.setTitle(param.getTitle());
        consultation.setOrderNum(param.getOrderNum());
        consultation.setCreateTime(new Date());
        consultation.setUpdateTime(new Date());
        consultationMapper.insert(consultation);
        cacheService.deleteIndexCache();
        return Ref.ok();
    }

    /** 修改 */
    public CommonResult modify(ConParam.ModifyParam param){
        Consultation consultation = consultationMapper.selectById(param.getId());
        if(consultation==null){
            return Ref.error("找不到咨询图片");
        }
        consultation.setImgUrl(param.getImgUrl());
        consultation.setTitle(param.getTitle());
        consultation.setOrderNum(param.getOrderNum());
        consultation.setUpdateTime(new Date());
        consultationMapper.updateById(consultation);
        cacheService.deleteIndexCache();
        return Ref.ok();
    }

    /** 获取表格 */
    public CommonResult tableList(ConParam.TableListParam param){

        List<Consultation> list = consultationMapper.list(param.getStart(), param.getNum());
        int count = consultationMapper.countList();


        List<ConVo.TableListVo> vos = new ArrayList<>();
        for(int i=0; i<list.size(); i++){
            Consultation con = list.get(i);
            ConVo.TableListVo vo = new ConVo.TableListVo();
            // 每页8条数据，第一页的前5条数据显示在首页
            if(i<5 && param.getStart()==0){
                vo.setIndexNum(i+1);
            }else{
                vo.setIndexNum(0);
            }
            vo.setId(con.getId());
            vo.setImgUrl(con.getImgUrl());
            vo.setTitle(con.getTitle());
            vo.setOrderNum(con.getOrderNum());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(con.getCreateTime()));
            vo.setUpdateTime(TimeUtil.formaterYYYYMMddHHmm(con.getUpdateTime()));
            vos.add(vo);
        }
        JSONArray array = new JSONArray();
        JSONObject json = null;
        for(int i=0; i<vos.size(); i++){
            if(i%4==0){
                json = new JSONObject();
                json.put("id", i);
                json.put("data", new JSONArray());
                array.add(json);
            }
            JSONArray data = json.getJSONArray("data");
            data.add(vos.get(i));
        }
        JSONObject obj = new JSONObject();
        obj.put("total", count);
        obj.put("rows", array);
        return Ref.ok(obj);
    }

    /** 删除 */
    public CommonResult delete(ConParam.DeleteParam param){
        Consultation consultation = consultationMapper.selectById(param.getId());
        if(consultation==null){
            return Ref.ok();
        }
        String imgUrl = consultation.getImgUrl();
        ossService.delete(imgUrl);
        consultationMapper.deleteById(param.getId());
        cacheService.deleteIndexCache();
        return Ref.ok();
    }

    /**前端分页显示图片列表*/
    public CommonResult webList(ConParam.WebListParam param){
        Integer page = param.getPage();
        Integer num = param.getNum();
        if(page==null){
            page = 1;
        }
        if(num==null){
            num = 12;
        }
        int start = (page - 1) * num;
        List<Consultation> list = consultationMapper.list(start, num);
        int count = consultationMapper.countList();
        List<String> imgs = new ArrayList<>();
        for(Consultation con : list){
            imgs.add(con.getImgUrl());
        }
        DataGrid<String> dataGrid = new DataGrid<>();
        dataGrid.setTotal(count);
        dataGrid.setRows(imgs);
        return Ref.ok(dataGrid);
    }
}
